package com.demo.interceptor;

import com.demo.constant.ErrorInfo;
import com.demo.controller.BaseController;
import com.demo.dto.AccessToken;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;

public class AccessTokenInterceptor implements Interceptor {

	@Override
	public void intercept(Invocation inv) {
		BaseController controller = (BaseController) inv.getController();
		String actionKey = inv.getActionKey();
		if (!"/user/getCode".equals(actionKey) && !"/user/login".equals(actionKey) && !"/".equals(actionKey)) {
			String token = controller.getPara("access_token", "");
			if ("".equals(token)) {
				controller.renderDataJson(ErrorInfo.ERR_NOT_LOGIN);
				return;
			}
			if (!AccessToken.validateToken(token)) {
				controller.renderDataJson(ErrorInfo.ERR_NOT_LOGIN);
				return;
			}
		}
		inv.invoke();
	}

}
